import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  Delete,
  UseGuards,
  Query,
} from '@nestjs/common';
import { CategoryService } from './category.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
@Controller('category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @Post()
  @UseGuards(JwtAuthGuard) // 确保 JWT 认证守卫在 RoleGuard 之前执行
  async create(@Body() createCategoryDto: CreateCategoryDto) {
    return await this.categoryService.create(createCategoryDto);
  }

  @Get()
  // @UseGuards(JwtAuthGuard) // 确保 JWT 认证守卫在 RoleGuard 之前执行
  async findAll(@Query() query: { page: number; pageSize: number }) {
    return await this.categoryService.findAll(query.page, query.pageSize);
  }

  @Get(':id')
  // @UseGuards(JwtAuthGuard) // 确保 JWT 认证守卫在 RoleGuard 之前执行
  async findOne(@Param('id') id: string) {
    return await this.categoryService.findOne(+id);
  }

  @Put(':id')
  @UseGuards(JwtAuthGuard) // 确保 JWT 认证守卫在 RoleGuard 之前执行
  async update(
    @Param('id') id: string,
    @Body() updateCategoryDto: UpdateCategoryDto,
  ) {
    return await this.categoryService.update(+id, updateCategoryDto);
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard) // 确保 JWT 认证守卫在 RoleGuard 之前执行
  async remove(@Param('id') id: string) {
    return await this.categoryService.remove(+id);
  }

  @Get(':id/link')
  async findLink(@Param('id') id: string) {
    return await this.categoryService.findLink(+id);
  }

  @Get(':title/search')
  async findByTitle(@Param('title') title: string) {
    return await this.categoryService.findByTitle(title);
  }

  @Get('get/count')
  async countLinks() {
    return await this.categoryService.countLinks();
  }
}
